﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using App.Models;
using App.Models.Pager;
using App.Views.ViewModels;

namespace App.Controllers
{
    public class FareWellServiceController : BaseController
    {
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult List(int? page, int? rows, string order)
        {
            dbe.ContextOptions.ProxyCreationEnabled = false;
            dbe.ContextOptions.LazyLoadingEnabled = false;

            int offset = (page ?? 1 - 1) * (rows ?? 10);
            PagedList<tb_FarewellRecords> list = null;
            if ("asc" == order)
            {
                list = dbe.tb_FarewellRecords.OrderBy(m => m.FarewellNO)
                                .ToPagedList(page ?? 1, rows ?? 10);
            }
            else
            {
                list = dbe.tb_FarewellRecords.OrderByDescending(m => m.FarewellNO)
                                .ToPagedList(page ?? 1, rows ?? 10);
            }
            PagedList<FarewellViewModel> data = new PagedList<FarewellViewModel>(
                                                list.CurrentPageIndex, list.PageSize, list.total);
            data.rows.AddRange(from f in list.rows
                               select new FarewellViewModel
                               {
                                   FarewellNO  = f.FarewellNO,
                                   ServiceNo = f.ServiceNo,
                                   DeadNO = dbe.tb_ServiceDetail.FirstOrDefault(m => m.ServiceNo == f.ServiceNo).DeadNO,
                                   UseTime = f.UseTime
                               });

            return Json(data, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Create()
        {
            var vmodel = GetServiceItemList("", Utils.FAREWELL_SERVICE_TYPE);
            return PartialView("UCServicesItemList", vmodel);
        }

        [HttpPost]
        public ActionResult Create([Bind(Exclude = "ItemList")]FarewellViewModel vmodel)
        {
            try
            {
                //保存服务费用
                string serviceNo = GenServiceNo();
                AddServiceItems(vmodel.DeadNO, serviceNo, vmodel.SelectedItemList);

                //保存告别厅使用记录
                var m = new tb_FarewellRecords
                {
                    FarewellNO = GenFarewellNO(),
                    ServiceNo = serviceNo,
                    UseTime = vmodel.UseTime,
                };
                dbe.tb_FarewellRecords.AddObject(m);                
                dbe.SaveChanges();
                CurrentDeader = vmodel.DeadNO;
                return Json(new { success = true }, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json(new { success = false, msg = "保存出错！" }, JsonRequestBehavior.AllowGet);
            }            
        }

        public ActionResult Edit(string id)
        {
            var model = dbe.tb_FarewellRecords.FirstOrDefault(m => m.FarewellNO == id);
            var vmodel = GetServiceItemList(model.ServiceNo, Utils.FAREWELL_SERVICE_TYPE);
            return PartialView("UCServicesItemList", vmodel);
        }

        [HttpPost]
        public ActionResult Edit(string id, [Bind(Exclude = "ItemList")]FarewellViewModel vmodel)
        {
            try
            {
                EditServiceItems(vmodel.SelectedItemList, vmodel.DeadNO, vmodel.ServiceNo);

                var model = dbe.tb_FarewellRecords.FirstOrDefault(m => m.FarewellNO == vmodel.FarewellNO);
                model.UseTime = vmodel.UseTime;
                dbe.SaveChanges();
                CurrentDeader = vmodel.DeadNO;
                return Json(new { success = true }, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json(new { success = false, msg = "添加出错！" }, JsonRequestBehavior.AllowGet);
            }   
        }
       
        [HttpPost]
        public ActionResult Delete(string ids)
        {
            try
            {
                string[] idList = ids.Split('&');
                foreach (var id in idList)
                {
                    var obj = dbe.tb_FarewellRecords.FirstOrDefault(m => m.FarewellNO == id);
                    dbe.tb_FarewellRecords.DeleteObject(obj);
                    //删除服务收费
                    DeleteServiceItems(obj.ServiceNo);
                }

                dbe.SaveChanges();
                var res = new { success = true };
                return Json(res, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                var res = new { success = false, msg = "删除出错！" };
                return Json(res, JsonRequestBehavior.AllowGet);
            }
        }        
    }
}
